package core

import org.apache.commons.lang.text.StrBuilder;

class PersonController {
	//static scaffold = true
	//static layout = 'main'
	def index () {

		def max =params.long('max')?:100
		def start = params.long('start')?:0
		render(view:'index', model: [persons: Person.list(max: max, offset:start),max: max, start: start])

		//render "<pre>hello: </pre>"
	}
	def show(long id){
		[person: Person.get(id)]

	}
	def save(long id){
		def person = new Person(params)
		if (person.save()){
			redirect(action: 'index')
			flash.content = "new person: ${person.id} was created"
		}
		else {
			render(view: 'create', model: [person: person])
			flash.error = "new person creation: Failed"
		}
	}
	def update (long id){
		
		def person =  Person.get(params.long('id'))
		person.properties = params
		if (person.save()){
			flash.content = "update succeeded"
			redirect(action:'index')
			// render(view:'edit', model:[person: person])*/
			//render text: "${params.id}"
		}
		else {
			flash.error = "update faild"
			render(view:'edit', model:[person: person])
		}

		
	}
	def edit(){
		[person: Person.get(params.long('id'))]
	}
	def delete(long id){
		def person = Person.get(id)
		person.delete()
		redirect(view: 'index')
	}

}
